﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Strings Exercise 3</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Function that finds how many times a substring is contained in a given text.</h1>
    <label for="str-input">String:</label>
    <input type="text" id="str-input" />
    <label for="text-input">Text:</label>
    <input type="text" id="text-input" />
    <button onclick="execute()">execute</button>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function findSubStr(str, text)
        {
            var strCount = 0;
            var searchPos = 0;

            str = str.toLowerCase();
            text = text.toLowerCase();

            do {
                var subIdx = text.indexOf(str, searchPos);
                if (subIdx >= 0) {
                    strCount++;
                    searchPos = subIdx + 1;
                }
            } while (subIdx >= 0);

            return strCount;
        }

        function execute()
        {
            var inpStr = jsConsole.read("#str-input");
            var inpText = jsConsole.read("#text-input");
            var count = findSubStr(inpStr, inpText);
            jsConsole.writeLine("String is found " + count + " " + (1 == count?"time":"times"));
        }
    </script>
</body>
</html>
